package cn.sxt.userscenter.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.dao.AAdminsDao;
import cn.sxt.userscenter.dao.ARoleDao;
import cn.sxt.userscenter.dao.IAUsersDao;
import cn.sxt.userscenter.dao.IAUsersExtendsDao;
import cn.sxt.userscenter.service.IUserService;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.ARole;
import cn.sxt.userscenter.users.pojo.AUserExtends;
import cn.sxt.userscenter.users.pojo.AUsers;

//用户中心实现类
@Service("userService")
public class UserServiceImpl implements IUserService{
	@Resource
	private AAdminsDao adminsDao;
	@Resource 
	private ARoleDao roleDao;
	@Resource 
	private IAUsersDao usersDao;
	@Resource 
	private IAUsersExtendsDao usersExtendsDao;
	
	
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.saveOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.updateOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		// TODO Auto-generated method stub
		return null ;
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.adminsDao.findCondList(condMap);
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.roleDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		int res = this.roleDao.saveOne(role);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0 )
		{
			/* 存储结果 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId() + "");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		int res = this.roleDao.updateOne(role);
		
		if(res > 0 )
		{
			/* 存储结果 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId() + "");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		int res = this.roleDao.deleteOne(role);
		
		if(res > 0 )
		{
			/* 存储结果 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId() + "");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			Page<ARole> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询数据 */
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			/* 设置总记录数 ,一定要在查询结果之后*/
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return roleList;
		}
		return this.roleDao.findCondList(condMap);
	}

	
//用户模块开始	
	public AUsers findOneUsersService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersDao.saveOne(users);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			//保存用户扩展信息
			AUserExtends usersExtend = new AUserExtends() ; 
			usersExtend.setUsersId(users.getId());
			usersExtend.setCreateTime(new Date());
			usersExtend.setUpdateTime(new Date());
			users.setUsersExtends(usersExtend);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersDao.updateOne(users);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersService(AUsers users) {
		// TODO Auto-generated method stub
		return null ;
	}

	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersDao.findCondList(condMap);
	}

//用户模块结束
// 用户扩展模块开始
	@Override
	public AUserExtends findOneUsersExtendsService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersExtendsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersExtendsService(AUserExtends usersExtends) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersExtendsDao.saveOne(usersExtends);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", usersExtends.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersExtendsService(AUserExtends usersExtends) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersExtendsDao.updateOne(usersExtends);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", usersExtends.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersExtendsService(AUserExtends usersExtends) {
		// TODO Auto-generated method stub
		return null ;
	}

	@Override
	public List<AUserExtends> findCondListUsersExtendsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersExtendsDao.findCondList(condMap);
	}
//用户扩展模块结束

	


	
}
